package com.example.basicknowledge.文本切换器;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.TextSwitcher;
import android.widget.TextView;
import android.widget.ViewSwitcher;

import com.example.basicknowledge.R;

import java.text.SimpleDateFormat;
import java.util.Date;

public class TextSwitcherActivity extends AppCompatActivity implements View.OnClickListener, ViewSwitcher.ViewFactory {
    private Button btn;
    private TextSwitcher myTextSwitcher;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_switcher_activity);

        btn = (Button)findViewById(R.id.btn);
        myTextSwitcher = (TextSwitcher)findViewById(R.id.myTextSwitcher);
        btn.setOnClickListener(this);
        myTextSwitcher.setFactory(this);
        myTextSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        myTextSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
    }

    @Override
    public void onClick(View view) {
        myTextSwitcher.setText("当前时间为：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date()));
    }

    @Override
    public View makeView() {
        TextView myTextView = new TextView(this);
        myTextView.setBackgroundColor(0xFFFFFFFF);
        myTextView.setTextColor(Color.RED);
        myTextView.setLayoutParams(new TextSwitcher.LayoutParams(WindowManager.LayoutParams.FILL_PARENT, WindowManager.LayoutParams.FILL_PARENT));
        return myTextView;
    }
}
